﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ECommerce.Model
{
    public class Objeto 
    {
        private int id;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public string Value
        {
            get { return id.ToString(); }
            set { id = Convert.ToInt32(value); }
        }
        public string Name
        {
            get { return this.ToString(); }
        }
        virtual public string Ruta
        {
            get { return this.ToString(); }
        }
        virtual public string NavigateUrl
        {
            get { return "/Obejto.aspx?Id=" + this.Id; }
        }
        virtual public string ShortDescription
        {
            get 
            {
                if (this.ToString().Length > 29)
                    return "..." + this.ToString().Substring(this.ToString().Length - 32);
                else
                    return this.ToString();
            }
        }
        private string denominación;
        public string Denominación
        {
            get { return denominación; }
            set { denominación = value; }
        }
        private System.Web.UI.WebControls.TreeNode treeNode;
        public System.Web.UI.WebControls.TreeNode TreeNode
        {
            get { return this.treeNode; }
            set { this.treeNode = value; }
        }
        public void LoadTreeNode(string Formato, string ImageUrl)
        {
            //Instancio el Nodo
            this.TreeNode = new System.Web.UI.WebControls.TreeNode();
            //Seteo la imagen del nodo
            if (ImageUrl != "")
                this.TreeNode.ImageUrl = ImageUrl;
            //Completo el campo Text
            if (Formato != "")
                this.TreeNode.Text = this.ToString();
            else
                this.TreeNode.Text = this.ToString();
            //Completo el campo Value con el Id
            this.TreeNode.Value = this.Id.ToString();
            //Defino la Url de Navegación
            this.TreeNode.NavigateUrl = "";
            //Completo el Tooltip con la ruta
            this.TreeNode.ToolTip = this.ToString();
        }
        virtual public void Load(int ident)
        {
            this.Id = ident;
            this.Load(ECommerce.DAO.Cache.Objeto.GetObjetoById(ident));
            if (this.Id == 0)
            {
                this.Id = ident;
                this.Load(ECommerce.DAO.SQL.Objeto.GetObjetoById(ident));
                ECommerce.DAO.Cache.Objeto.SetObjeto(this);
            }
        }
        virtual public void Load(ECommerce.Model.Objeto Objeto)
        {
            if (Objeto != null)
            {
                this.Id = Objeto.Id;
                this.Denominación = Objeto.Denominación;
            }
            else
            {
                this.Id = 0;
                this.Denominación = "";
            }
        }
        virtual public void Load(System.Data.DataRow dr)
        {
            ECommerce.Factory.Objeto.Load(this, dr);
            ECommerce.DAO.Cache.Objeto.SetObjeto(this);
        }
        virtual public void Save()
        {
            this.Id = ECommerce.DAO.SQL.Objeto.SetObjeto(this);
            this.Id = ECommerce.DAO.Cache.Objeto.SetObjeto(this);
        }
        public override string ToString()
        {
            return this.Denominación;
        }
        virtual public void Del()
        {
            this.Id = ECommerce.DAO.SQL.Objeto.DelObjeto(this);
            this.Id = ECommerce.DAO.Cache.Objeto.DelObjeto(this);
        }
        virtual public System.Web.UI.WebControls.ListItem ToListItem()
        {
            System.Web.UI.WebControls.ListItem result = new System.Web.UI.WebControls.ListItem();
            result.Text = this.ToString();
            result.Value = this.Value.ToString();
            return result;
        }
    }
}
